package edu.hunau.dom4j;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hb
 */
public class XmlDom4jParseWeb {

    @Test

    public void test01() throws Exception{
        Map map = new HashMap();
        map.put("xis","http://xmlns.jcp.org/xml/ns/javaee");
        //1.创建Reader对象
        SAXReader saxReader = new SAXReader();
        saxReader.getDocumentFactory().setXPathNamespaceURIs(map);

        //2.加载xml
        Document document = saxReader.read(new File("./src/main/resources/webdemo.xml"));
        //3. 获得servlet-name的值
        List<Node> nodes = document.selectNodes("//xis:servlet-name");
        String servletName01 = nodes.get(0).getText();
        System.out.println(servletName01);

        //获得servlet-class的值
        String servletClass = document.selectSingleNode("//xis:servlet-class").getText();
        String urlPattern = document.selectSingleNode("//xis:url-pattern").getText();
        System.out.println(servletClass);
        System.out.println(urlPattern);

    }
}
